package com.zxtc.wh.dao;


import com.zxtc.wh.pojo.UserInofPojo;
import com.zxtc.wh.utils.SqlUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/13.
 */
public class UserInofDaoImpl implements IUserInfoDao {
    public void insert(UserInofPojo info) throws SQLException, ClassNotFoundException {
        final String sqlCmd = "INSERT INTO user_info (userName,remark,userPhotPath)" +
                "VALUES (?,?,?);";

        PreparedStatement statement = SqlUtil.getInstance().getStatement(sqlCmd);
        statement.setString(1, info.getUserName());
        statement.setString(2, info.getRemark());
        statement.setString(3, info.getUserPhotPath());

        statement.executeUpdate();
        statement.close();
    }

    public List<UserInofPojo> getUserInofPojos() throws SQLException, ClassNotFoundException {
        final String sqlCmd = "select * from user_info";

        PreparedStatement statement = SqlUtil.getInstance().getStatement(sqlCmd);
        ResultSet resultSet = statement.executeQuery();

        List<UserInofPojo> list = new LinkedList<>();
        while (resultSet.next()) {
            UserInofPojo pojo = new UserInofPojo();
            pojo.setUserId(resultSet.getLong("userId"));
            pojo.setUserPhotPath(resultSet.getString("userPhotPath"));
            pojo.setUserName(resultSet.getString("userName"));
            pojo.setRemark(resultSet.getString("remark"));

            list.add(pojo);
        }
        resultSet.close();
        statement.close();

        return list;
    }
}
